package com.woniu.darryringmall.admin.controller;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.darryringmall.front.entity.*;
import com.woniu.darryringmall.front.service.MessageService;
import com.woniu.darryringmall.front.service.OrderService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import com.woniu.darryringmall.utils.ResultStateCode;
import com.woniu.darryringmall.utils.SystemMessageTempleat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController("adminOrder")
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private MessageService messageService;

    @RequestMapping("list/{current}")
    public ResponseData getAll(@PathVariable int current, Order order) {
        /*if (current == null) {
            current = 1;
        }*/
        Page<Order> page = new Page<>(current, 2);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        if (order.getOrderNo() != null && order.getOrderNo().trim().length() != 0) {
//            queryWrapper.like("orderno", order.getOrderNo());
//        }
//        if (order.getUserId() != 0) {
//            queryWrapper.eq("userid", order.getUserId());
//        }
//        if (order.getAccept() != null && order.getAccept().trim().length() != 0) {
//            queryWrapper.like("accept", order.getAccept());
//        }
//        if (order.getTelephone() != null && order.getTelephone().trim().length() != 0) {
//            queryWrapper.like("telephone", order.getTelephone());
//        }
//        if (order.getOrderTime() != null) {
//            queryWrapper.ge("ordertime", order.getOrderTime());
//        }
        if (order.getStatus() != null) {
            queryWrapper.eq("status", order.getStatus());
        }
        page = orderService.page(page, queryWrapper);
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", page);
    }

    /**
     * 删除订单，和对应的订单详情
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public ResponseData del(@PathVariable int id) {
        orderService.del(id);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 更新发货状态
     * @param id
     * @return
     */
    @RequestMapping("update/{id}/{orderNo}")
    public ResponseData update(@PathVariable int id , HttpSession session,@PathVariable String orderNo) {
        /*UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", "3");
        updateWrapper.eq("id", id);*/
        orderService.update(id,"5");


        //发送系统消息
        Message message = new Message();
        User user = (User) session.getAttribute("user");
        message.setUserId(2);
        message.setMessage(SystemMessageTempleat.deliver(orderNo));
        message.setOrderNo(orderNo);
        message.setLabel(SystemMessageTempleat.deliver);
        message.setStatus("未读");
        messageService.save(message);
        //缓存消息数量
        redisTemplate.opsForValue().increment("tom");
        return ResponseUtils.SUCCESS;
    }

    /**
     * 按条件查询订单
     *
     */

}
